﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;
using Newtonsoft.Json.Linq;

namespace LdCms.Web.Controllers.API.Member.V1
{
    using LdCms.IBLL.Member;
    using LdCms.Web.Services;
    using LdCms.Common.Utility;
    using LdCms.Common.Security;
    using LdCms.Common.Net;
    using LdCms.Common.Time;
    using LdCms.Common.Extension;
    using LdCms.Common.Token;
    using LdCms.EF.DbModels;
    using LdCms.Common.Enum;

    /// <summary>
    /// 前后端分离API
    /// uuid：460e64203493444ba27d4fc7ad7efae8
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// </summary>
    [ApiVersion("1.0")]
    [ControllerName("member")]
    public partial class MemberController : BaseApiController
    {
        private readonly IBaseApiManager BaseApiManager;
        private readonly IHttpContextAccessor Accessor;
        private readonly IAccountService AccountService;
        private readonly IAccountAccessTokenService AccountAccessTokenService;

        private readonly IPointRecordService PointRecordService;
        private readonly ILoginLogsService LoginLogsService;
        public MemberController(IBaseApiManager BaseApiManager, IHttpContextAccessor Accessor, IAccountService AccountService, IAccountAccessTokenService AccountAccessTokenService, IPointRecordService PointRecordService, ILoginLogsService LoginLogsService) : base(BaseApiManager)
        {
            this.BaseApiManager = BaseApiManager;
            this.Accessor = Accessor;
            this.AccountService = AccountService;
            this.AccountAccessTokenService = AccountAccessTokenService;
            this.PointRecordService = PointRecordService;
            this.LoginLogsService = LoginLogsService;
        }

        private bool AddPointRecordPro(string companyId, string memberId, int classId, int typeId, string body)
        {
            try
            {
                string className = EnumHelper.GetName<ParamEnum.MemberPointClass>(classId);
                string typeName = EnumHelper.GetName<ParamEnum.MemberPointType>(typeId);
                int points = 0;
                string tasksId = "";
                string account = "sys";
                string nickname = "sys";
                string ipAddress = Net.Ip;
                string remark = "";
                bool state = true;
                return PointRecordService.AddPointRecordPro(SystemID, companyId, memberId, classId, className, typeId, typeName, points, tasksId, body, account, nickname, ipAddress, remark, state);
            }
            catch (Exception)
            {
                return false;
            }
        }
        private bool SaveLoginLogs(string memberId, string account, string nickname,bool result)
        {
            try
            {
                string description = "登录成功！";
                return LoginLogsService.SaveLoginLogs(new Ld_Member_LoginLogs()
                {
                    SystemID = SystemID,
                    CompanyID = InterfaceAccount.CompanyID,
                    MemberID = memberId,
                    ClientID = (int)ParamEnum.Client.Empty,
                    Account = account,
                    NickName = nickname,
                    IpAddress = Net.Ip,
                    IsResult = result,
                    Description = description,
                    CreateDate = DateTime.Now
                });
            }
            catch (Exception)
            {
                return false;
            }
        }

    }
}